package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.device.dao.DeviceDao;
import com.zretc.device.entity.Device;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeviceAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textDevCode;
	private JTextField textHighNum;
	private JTextField textLowNum;
	private JTextField textTotalNum;
	private JTextField textNowNum;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeviceAddDialog dialog = new DeviceAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DeviceAddDialog() {
		setBounds(100, 100, 675, 506);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("设备添加");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(221, 13, 182, 47);
		contentPanel.add(label);
		
		textDevCode = new JTextField();
		textDevCode.setFont(new Font("宋体", Font.PLAIN, 25));
		textDevCode.setColumns(10);
		textDevCode.setBounds(247, 73, 250, 30);
		contentPanel.add(textDevCode);
		
		JLabel label_1 = new JLabel("设备编号");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(113, 73, 120, 30);
		contentPanel.add(label_1);
		
		JTextField textDevName = new JTextField();
		textDevName.setFont(new Font("宋体", Font.PLAIN, 25));
		textDevName.setColumns(10);
		textDevName.setBounds(247, 119, 250, 30);
		contentPanel.add(textDevName);
		
		JLabel label_2 = new JLabel("设备名称");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(113, 119, 120, 30);
		contentPanel.add(label_2);
		
		textHighNum = new JTextField();
		textHighNum.setFont(new Font("宋体", Font.PLAIN, 25));
		textHighNum.setColumns(10);
		textHighNum.setBounds(247, 205, 250, 30);
		contentPanel.add(textHighNum);
		
		JLabel label_3 = new JLabel("最大库存");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(113, 205, 120, 30);
		contentPanel.add(label_3);
		
		textLowNum = new JTextField();
		textLowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		textLowNum.setColumns(10);
		textLowNum.setBounds(247, 248, 250, 30);
		contentPanel.add(textLowNum);
		
		JLabel label_4 = new JLabel("最小库存");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(113, 248, 106, 30);
		contentPanel.add(label_4);
		
		textTotalNum = new JTextField();
		textTotalNum.setFont(new Font("宋体", Font.PLAIN, 25));
		textTotalNum.setColumns(10);
		textTotalNum.setBounds(247, 291, 250, 30);
		contentPanel.add(textTotalNum);
		
		JLabel label_5 = new JLabel("库存总数");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(113, 291, 120, 30);
		contentPanel.add(label_5);
		
		textNowNum = new JTextField();
		textNowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		textNowNum.setColumns(10);
		textNowNum.setBounds(247, 162, 250, 30);
		contentPanel.add(textNowNum);
		
		JLabel label_6 = new JLabel("现有库存");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(113, 162, 120, 30);
		contentPanel.add(label_6);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 设备id
				String device_code=textDevCode.getText();
				// 设备名称
				String device_name=textDevName.getText();
				// 现有库存,转型
				String tnow_num=textNowNum.getText();
				// 最大库存
				String thigh_num=textHighNum.getText();
				// 最小库存
				String tlow_num=textLowNum.getText();
				// 总数
				String ttotal_num=textTotalNum.getText();
				//判断输入的值
				if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				} else if (device_name.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "名称不能为空");
				} else if (tnow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "现有库存不能为空");
				} else if (thigh_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最大库存不能为空");
				} else if (tlow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最低库存不能为空");
				} else if (ttotal_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "库存总数不能为空");
				}else {
					// 现有库存,转型
					int now_num=Integer.parseInt(tnow_num);
					// 最大库存
					int high_num=Integer.parseInt(thigh_num);
					// 最小库存
					int low_num=Integer.parseInt(tlow_num);
					// 总数
					int total_num=Integer.parseInt(ttotal_num);
					//创建对象
					Device device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
					DeviceDao dd = new DeviceDao();
					//调用增加的方法
					boolean flag=dd.addDevice(device);
					//对返回值进行判断
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "添加成功");
					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "该设备编号已存在");
					}
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(337, 351, 150, 38);
		contentPanel.add(button);
		
		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textDevCode.setText("");
				textDevName.setText("");
				textHighNum.setText("");
				textLowNum.setText("");
				textNowNum.setText("");
				textTotalNum.setText("");
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(136, 351, 150, 38);
		contentPanel.add(button_1);
		//设置窗口居中
		setLocationRelativeTo(null);
	}
}
